namespace Day27Auth.Data.Migrations
{
    using Day27Auth.Data.Models;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<Day27Auth.Data.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(Day27Auth.Data.ApplicationDbContext context)
        {
            UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(store);

            RoleStore<Role> roleStore = new RoleStore<Role>(context);
            RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);

            if (!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new Role
                {
                    Name = "Admin"
                });
            }

            if (!roleManager.RoleExists("User"))
            {
                roleManager.Create(new Role
                {
                    Name = "User"
                });
            }

            ApplicationUser nick = userManager.FindByName("Nick");

            if (nick == null)
            {
                userManager.Create(new ApplicationUser
                {
                    UserName = "Nick",
                    Email = "nick@codercamps.com"
                }, "123456");

                nick = userManager.FindByName("Nick");
                userManager.AddToRole(nick.Id, "Admin");
            }

            ApplicationUser justin = userManager.FindByName("Justin");

            if (justin == null)
            {
                userManager.Create(new ApplicationUser
                {
                    UserName = "Justin",
                    Email = "justin@marvel.com"
                }, "123456");

                justin = userManager.FindByName("Justin");
                userManager.AddToRole(justin.Id, "User");
            }

            context.Todos.AddOrUpdate(t => t.Subject,
                new Todo { Subject = "Marvel Comics", Message = "Buy 20 comic books!", Timestamp = DateTime.Now, UserId = justin.Id },
                new Todo { Subject = "Hate Peanut Butter", Message = "I don't like this stuff man.", Timestamp = DateTime.Now, UserId = justin.Id },
                new Todo { Subject = "Peanut Butter Rocks!", Message = "You want super powers you better be eatin some PB yo!", Timestamp = DateTime.Now, UserId = nick.Id }
            );
        }
    }
}
